<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房间类型" prop="typeid">
      <el-select v-model="dataForm.typeid" style="width: 100%" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.typename"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="开始时间" prop="starttime">
      <el-date-picker
        v-model="dataForm.starttime"
        type="date"
        placeholder="选择日期"
        style="width: 100%"
        value-format="yyyy-MM-dd">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="endtime">
      <el-date-picker
        v-model="dataForm.endtime"
        type="date"
        placeholder="选择日期"
        style="width: 100%"
        value-format="yyyy-MM-dd">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="价格" prop="prices">
      <el-input v-model="dataForm.prices" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="subable" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        subable: false,
        visible: false,
        options: [],
        dataForm: {
          id: 0,
          typeid: '',
          starttime: '',
          endtime: '',
          prices: '',
          remark: ''
        },
        dataRule: {
          typeid: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          starttime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          prices: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.$http({
          url: this.$http.adornUrl(`/roomtype/roomtype/selectlist`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.typeList
          }
        })

        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.subable = false
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/typeprice/typeprice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.typeid = data.typeprice.typeid
                this.dataForm.starttime = data.typeprice.starttime
                this.dataForm.endtime = data.typeprice.endtime
                this.dataForm.prices = data.typeprice.prices
                this.dataForm.remark = data.typeprice.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.subable = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/typeprice/typeprice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'typeid': this.dataForm.typeid,
                'starttime': this.dataForm.starttime,
                'endtime': this.dataForm.endtime,
                'prices': this.dataForm.prices,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.subable = false
          }
        })
      }
    }
  }
</script>
